#include "EffectEntry.h"
#include "Character/Player.h"
#include "base/core/com_define.h"
#include "Common/TableData/EffectEffectCfg.h"
#include "Common/CampDefine.h"
#include "Character/SkillPart.h"
#include "Character/monster/MonsterRefCfg.h"
#include "Character/newAI/AiLogicUtility.h"
#include "Character/CreatureMgr.h"
#include "PlaneMgr/PlaneMgr.h"
#include "Common/TableData/AreaAreaCfg.h"
#include "Character/monster/Monster.h"
int32_t EffectAddBuff::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	if (nullptr == pCreature)
	{
		LogErrFmtPrint("EffectAddBuff::OnEffect..nullptr == pCreature..");
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectAddBuff::OnEffect..nullptr == pEffectCfg..cid:%lu ",pCreature->GetCid());
		return RET_FAIL;
	}
	//注意，此处只处理BUFF类技能
	SkillPart* pSkillPart = dynamic_cast<SkillPart*>(pCreature->GetPart(PART_SKILL));
	if (nullptr == pSkillPart)
	{
		return RET_FAIL;
	}
	if (pEffectCfg->vec_eparam.size() < 2)
	{
		LogErrFmtPrint("EffectAddBuff::OnEffect..nullptr == pEffectCfg..cid:%lu,effectid:%d ", pCreature->GetCid(),pEffectCfg->effectId);
		return RET_FAIL;
	}

	if (pEffectCfg->vec_eparam[1]==0)
	{
		if (!pSkillPart->BeginUseSkillByItem(pEffectCfg->vec_eparam[0], 1))
		{
			return RET_FAIL;
		}
	}
	else
	{
		if (!pSkillPart->BeginUseSkillByItem(pEffectCfg->vec_eparam[0], pEffectCfg->vec_eparam[1]))
		{
			return RET_FAIL;
		}
	}
	
	return RET_SUCCESS;
}

int32_t EffectAddBuff::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	Player* pPlayer = dynamic_cast<Player*>(pCreature);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("EffectAddBuff::RemoveEffect..nullptr == pPlayer..");
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectAddBuff::RemoveEffect..nullptr == pEffectCfg..cid:%lu ", pPlayer->GetCid());
		return RET_FAIL;
	}
	if (pEffectCfg->vec_eparam.size() < 1)
	{
		LogErrFmtPrint("EffectAddBuff::RemoveEffect..pEffectCfg->vec_eparam.size() < 1..cid:%lu,effectid:%d ", pCreature->GetCid(), pEffectCfg->effectId);
		return RET_FAIL;
	}

	SkillPart* pSkillPart = dynamic_cast<SkillPart*>(pPlayer->GetPart(PART_SKILL));
	if (nullptr == pSkillPart)
	{
		return RET_FAIL;
	}	

	pSkillPart->RemoveSkill(pEffectCfg->vec_eparam[0],false);

	return RET_SUCCESS;
}

int32_t EffectTriggerMission::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	Player* pPlayer = dynamic_cast<Player*>(pCreature);
	if (nullptr == pPlayer)
	{
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectAddBuff::RemoveEffect..nullptr == pEffectCfg..cid:%lu ", pPlayer->GetCid());
		return RET_FAIL;
	}
	if (pEffectCfg->vec_eparam.size() < 1)
	{
		LogErrFmtPrint("EffectAddBuff::RemoveEffect..pEffectCfg->vec_eparam.size() < 1..cid:%lu,effectid:%d ", pPlayer->GetCid(),pEffectCfg->effectId);
		return RET_FAIL;
	}

	MissionPart* pMissionPart = dynamic_cast<MissionPart*>(pPlayer->GetPart(PART_MISSION));
	if (nullptr == pMissionPart)
	{
		return RET_FAIL;
	}

	pMissionPart->AcceptMissionByEffect(pEffectCfg->vec_eparam[0]);

	return RET_SUCCESS;
}

int32_t EffectTriggerMission::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	return RET_SUCCESS;
}

//刷怪效果
int32_t EffectTriggerRefreshMonster::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	if (nullptr == pCreature)
	{
		LogErrFmtPrint("EffectTriggerRefreshMonster::OnEffect..nullptr == pCreature");
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectTriggerRefreshMonster::OnEffect..nullptr == pEffectCfg..cid:%lu ", pCreature->GetCid());
		return RET_FAIL;
	}
	if (pEffectCfg->vec_eparam.size() < 5)
	{
		LogErrFmtPrint("EffectTriggerRefreshMonster::OnEffect..pEffectCfg->vec_eparam.size() < 5,cid:%lu,effectid:%d ",pCreature->GetCid(),pEffectCfg->effectId);
		return RET_FAIL;
	}
	//先取出该配置,根据类型来创建怪物
	MonsterExtendInfo stExtendInfo ;
	if (pEffectCfg->vec_eparam[4] > 0 )
	{
		stExtendInfo.set_mastercid(pCreature->GetCid());
	}



	if (pEffectCfg->vec_eparam[3]==1)
	{
		stExtendInfo.set_level(pCreature->GetAttr(C_LEVEL));
	}
	else if (pEffectCfg->vec_eparam[3] == 2) // 场景最高等级
	{
		stExtendInfo.set_level(1);  //这个要场景中配置
	}
	else if (pEffectCfg->vec_eparam[3] == 3) //跟随队伍
	{
		stExtendInfo.set_level(pCreature->GetAttr(C_LEVEL));
	}

	
	for (int i = 0; i<pEffectCfg->vec_eparam[2];++i)
	{
		Point3<float> tmpPos;
		uint32_t sceneid = pCreature->GetSceneId();
		if (pEffectCfg->vec_eparam[1]>0) //从区域取怪
		{

			tmpPos = g_GetMapLocationCfg()->RandPosInArea(pEffectCfg->vec_eparam[1]);

			const AreaAreaCfgInfo * pArea = g_GetAreaAreaCfgTable()->GetAreaAreaCfgInfo(pEffectCfg->vec_eparam[1]);
			if (pArea)
			{
				sceneid = pArea->belongToSceneID;
			}
			
		}
		else
		{
			tmpPos = AiLogicUtility::GetNearstPos(pCreature->GetMapId(),pCreature->GetPos());
		}
		Point3<float> curDir = Point3<float>(0,0,1);
		//创建怪物
		MonsCreateParam createParam;
		createParam.createType = EMonsCreateType_Effect;
		g_GetCreatureMgr()->CreateMonster(pEffectCfg->vec_eparam[0], sceneid, tmpPos, createParam,curDir, -1, &stExtendInfo, pCreature->GetCid());
	}

	return RET_SUCCESS;
}
int32_t EffectTriggerRefreshMonster::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	return RET_SUCCESS;
}

int32_t EffectTriggerRefreshCollect::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	if (nullptr == pCreature)
	{
		LogErrFmtPrint("EffectTriggerRefreshCollect::OnEffect..nullptr == pCreature");
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectTriggerRefreshCollect::OnEffect..nullptr == pEffectCfg..cid:%lu ", pCreature->GetCid());
		return RET_FAIL;
	}

	if (pEffectCfg->vec_eparam.size() < 3)
	{
		LogErrFmtPrint("EffectTriggerRefreshCollect::OnEffect..pEffectCfg->vec_eparam.size() < 3,cid:%lu,effectid:%d ", pCreature->GetCid(),pEffectCfg->effectId);
		return RET_FAIL;
	}
	for (int i = 0; i<pEffectCfg->vec_eparam[2]; ++i)
	{
		Point3<float> tmpPos;
		uint32_t sceneid = pCreature->GetSceneId();
		if (pEffectCfg->vec_eparam[1]>0) //从区域取怪
		{

			tmpPos = g_GetMapLocationCfg()->RandPosInArea(pEffectCfg->vec_eparam[1]);
			const AreaAreaCfgInfo * pArea = g_GetAreaAreaCfgTable()->GetAreaAreaCfgInfo(pEffectCfg->vec_eparam[1]);
			if (pArea)
			{
				sceneid = pArea->belongToSceneID;
			}
		}
		else
		{
			tmpPos = AiLogicUtility::GetNearstPos(pCreature->GetMapId(), pCreature->GetPos());
		}
		//创建怪物
		g_GetCreatureMgr()->CreateObject(pEffectCfg->vec_eparam[0], sceneid, tmpPos, -1);
	}
	return RET_SUCCESS;
}
int32_t EffectTriggerRefreshCollect::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	return RET_SUCCESS;
}


int32_t EffectTriggerRefreshPlane::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	//创建位面
	//std::vector<uint32_t>  vNpcConfigID;
	//for (int i = 0; i < (int)pEffectCfg->vec_eparam.size();++i)
	//{
	//	vNpcConfigID.push_back(pEffectCfg->vec_eparam[i]);
	//}
	//if (g_GetPlaneMgr()->CreatePlane(pCreature->GetCid(), vNpcConfigID)>0)
	//{
	//	return RET_SUCCESS;
	//}

	return RET_FAIL;
}
int32_t EffectTriggerRefreshPlane::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	return RET_SUCCESS;
}


int32_t EffectTriggerTrance::OnEffect(Creature *pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	if (nullptr == pCreature)
	{
		LogErrFmtPrint("EffectTriggerTrance::OnEffect..nullptr == pCreature");
		return RET_FAIL;
	}
	if (nullptr == pEffectCfg)
	{
		LogErrFmtPrint("EffectTriggerTrance::OnEffect..nullptr == pEffectCfg..cid:%lu ", pCreature->GetCid());
		return RET_FAIL;
	}

	if (pEffectCfg->vec_eparam.size() < 1)
	{
		LogErrFmtPrint("EffectTriggerTrance::OnEffect..pEffectCfg->vec_eparam.size() < 1,cid:%lu,effectid:%d ", pCreature->GetCid(),pEffectCfg->effectId);
		return RET_FAIL;
	}

	if (pEffectCfg->vec_eparam[0] > 0) //传送到的区域
	{

		const AreaAreaCfgInfo * pArea = g_GetAreaAreaCfgTable()->GetAreaAreaCfgInfo(pEffectCfg->vec_eparam[0]);
		if (pArea)
		{
			Point3<float> pos = g_GetMapLocationCfg()->RandPosInArea(pEffectCfg->vec_eparam[0]);
			uint32_t sceneid = pArea->belongToSceneID;
			STransParam transParam;
			transParam.srcMapId = pCreature->GetMapId();
			transParam.transType = ETransType_Effect;
			pCreature->TransScene(sceneid, pos, sceneid, transParam);
		}
	}

	return RET_SUCCESS;
}


int32_t EffectTriggerTrance::RemoveEffect(Creature* pCreature, const EffectEffectCfgInfo* pEffectCfg)
{
	return RET_SUCCESS;
}
	
